string resolverURL = "http://127.0.0.1/sl-www-integration/eval.php?equation=";
integer listener = 0;
key http_request_id;
key avatar;


default
{
    state_entry()
    {
        llSetText("Touch to send an equation", <1,1,1>, 1);
    }
    
    touch_start(integer howmany)
    {
        if (listener == 0)
        {
            avatar = llDetectedKey(0);
            listener = llListen(0, "", avatar, "");
            llSetText("In use by " + llDetectedName(0), <1,0,0>,1);
            llWhisper(0, "Type your equation; touch to reset");
            llSetTimerEvent(360.0);
        }
        else if (avatar == llDetectedKey(0))
        {
            llResetScript();
        }
        else
            llWhisper(0, "In use");
    }
    
    timer()
    {
        llResetScript();
    }

    listen(integer channel, string name, key id, string message)
    {
        http_request_id = llHTTPRequest(resolverURL + llEscapeURL(message), [], "");
    }

    http_response(key request_id, integer status, list metadata, string body)
    {
        if (request_id == http_request_id)
        {
            if (status == 200)
            {
                // The answer will come in plain text on "body", just echo it 
 
                llInstantMessage(avatar, body);
            }
            else // HTTP Request failed
                llInstantMessage(avatar, "Error " + (string)status + ": " + body);
        }
    }

}